package demo2;

import java.util.LinkedList;
import java.util.Objects;
import java.util.Stack;

public class Test {
    public static void main1(String[] args) {
        String string = "[]{}";
        System.out.println(isValid(string));
    }
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        //1、将字符串转为字符数组
        char[] ch = s.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if(ch[i] == '(' || ch[i] == '{' || ch[i] == '[' ) {
                stack.push(ch[i]);
            }else {
                if(stack.empty()) {
                    return false;
                }
                if(stack.peek() == '(' && ch[i] == ')' || stack.peek() == '{' && ch[i] == '}' || stack.peek() == '[' && ch[i] == ']') {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(!stack.empty()) {
            return false;
        }
        return true;
    }
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String string : tokens) {
            if(!isOperations(string)) {
                stack.push(Integer.parseInt(string));
            }else {
                int right = stack.pop();
                int left = stack.pop();
                switch (string) {
                    case "+" -> stack.push(left + right);
                    case "-" -> stack.push(left - right);
                    case "*" -> stack.push(left * right);
                    case "/" -> stack.push(left / right);
                }
            }
        }
        return stack.pop();
    }
    private boolean isOperations(String string) {
        return string.equals("+") || string.equals("-") || string.equals("*") || string.equals("/");
    }
    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while (!stack.empty() && j < popV.length && stack.peek() == popV[j]){
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }

    public static void main2(String[] args) {
        int[] pushV = {2,1,0};
        int[] popV = {1,2,0};
        System.out.println(IsPopOrder(pushV, popV));
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(512);
        minStack.push(-1024);
        minStack.push(-1024);
        minStack.push(512);
        minStack.pop();
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.getMin());
    }
}
class MinStack {
    Stack<Integer> stack;
    Stack<Integer> minStack;
    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }
    public void push(int val) {
        stack.push(val);
        if(minStack.empty()) {
            minStack.push(val);
        }else {
            if(val <= minStack.peek()) {
                minStack.push(val);
            }
        }
    }
    public void pop() {
        int popVal = stack.pop();
        if(popVal == minStack.peek()) {
        //if(Objects.equals(stack.pop(), minStack.peek())) {
            minStack.pop();
        }
    }
    public int top() {
        return stack.peek();
    }
    public int getMin() {
        return minStack.peek();
    }

}
